package com.ruoyi.driver.user.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.driver.user.service.DriverUserService;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.management.basics.controller.BaseExtController;
import com.ruoyi.management.basics.ro.GetAssessmentParam;
import com.ruoyi.management.basics.ro.GetEmployeesParam;
import com.ruoyi.management.basics.ro.GetVehiclesInformationListParam;
import com.ruoyi.management.basics.service.ITCarService;
import com.ruoyi.management.basics.service.ITCompanyService;
import com.ruoyi.management.basics.service.ITEmployeeService;
import com.ruoyi.management.basics.vo.GetBannerListVo;
import com.ruoyi.management.basics.vo.GetDriverDetailVo;
import com.ruoyi.management.basics.vo.GetVehiclesInformationListVo;
import com.ruoyi.management.mock.ro.GetNoticeListParam;
import com.ruoyi.management.mock.service.ITNoticeService;
import com.ruoyi.management.mock.vo.GetNoticeByIdVo;
import com.ruoyi.management.mock.vo.GetNoticeListVo;
import com.ruoyi.passenger.notice.ro.GetDriverHnoticeListParam;
import com.ruoyi.passenger.notice.ro.GetHnoticeListParam;
import com.ruoyi.supervise.collectemployee.ro.GetCollectEmployeeParam;
import com.ruoyi.supervise.collectemployee.service.CollectEmployeeService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 驾驶员信息
 * 
 * @author ruoyi
 */
@RestController
@Api(tags = "驾驶员端--公告管理")
@RequestMapping("/driver/info")
public class DriverUserController extends BaseExtController
{
    @Resource
    private ITNoticeService tNoticeService;

    @Autowired
    private ITEmployeeService employeeService;

    @Autowired
    private ITCompanyService companyService;

    @Autowired
    private ITCarService tCarService;

    @Autowired
    private TokenService tokenService;
    @Autowired
    private DriverUserService driverUserService;


    /**
     * {TODO}
     *驾驶员下拉选框
     * @param  * @param param
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @throws
     * @Author: xly
     */
    @ApiOperation(value = " 驾驶员下拉选框", notes = "驾驶员下拉选框")
    @GetMapping("get-drivers")
    public AjaxResult getDrivers(@Valid GetEmployeesParam param){
        return AjaxResult.success(employeeService.getDrivers(param));
    }
    /**
     * 获取驾驶员详细信息
     */
    @ApiOperation(value = " 获取驾驶员详细信息", notes = "获取驾驶员详细信息")
    @GetMapping("getDriverInfo")
    public AjaxResult authRole()
    {
        AjaxResult ajax = AjaxResult.success();
        LoginUser loginUser = getLoginUser();

        if (null!= loginUser)
        {
            String username = loginUser.getUsername();
            GetDriverDetailVo vo =  employeeService.getEmployeeDetailByPhone(username);
            ajax.put("user",vo);
            return ajax;
        }

        ajax.put("user",new GetDriverDetailVo());
        return ajax;
    }
    /**
     * banner查询 -驾驶员端
     */
    @ApiOperation(value = " banner查询 -驾驶员端", notes = "banner查询 -驾驶员端")
    @GetMapping("/get_banner_list")
    public TableDataInfo<List<GetBannerListVo>> getBannerList() {
        List<GetBannerListVo> list = tNoticeService.getBannerList();
        return getDataTable(list);
    }

    /**
     * 公告查询 带分页
     */
    @ApiOperation(value = "  公告查询 带分页", notes = " 公告查询 带分页")
    @GetMapping("/get_notice_list")
    public TableDataInfo<List<GetNoticeByIdVo>> list(@Valid GetDriverHnoticeListParam param) {
        startPage();
        param.setCompanyId(getCompanyId());
        List<GetNoticeByIdVo> list = tNoticeService.getNoticeList(param);
        return getDataTable(list);
    }

    @ApiOperation(value = "驾驶员端退出")
    @GetMapping("/loginout")
    public void loginout(HttpServletRequest request, HttpServletResponse response){
            LoginUser loginUser = tokenService.getLoginUser(request);
            if (StringUtils.isNotNull(loginUser))
            {
                String userName = loginUser.getUsername();
                // 删除用户缓存记录
                tokenService.delLoginUser(loginUser.getToken());
                // 记录用户退出日志
                AsyncManager.me().execute(AsyncFactory.recordLogininfor(userName, Constants.LOGOUT, "退出成功"));
            }
            ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(HttpStatus.SUCCESS, "退出成功")));
    }

    @ApiOperation(value = " 驾驶员考核")
    @GetMapping("get-collectemployee")
    public AjaxResult getCollectemployee(@Valid GetAssessmentParam param){
        return AjaxResult.success(driverUserService.getCollectemployee(param,getLoginUser().getUsername()));
    }


}
